﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using HelloXNA2.Component._3D._3DModel1;


namespace HelloXNA2.Component._3D.Collision
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Demo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private MyCamera _camera;

        private MyModel _modelSystem;

        private MyModel _modelUser;

        bool _collided = false;

        public Demo(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            _camera = new MyCamera(Game, new Vector3(0, 0, 50), Vector3.Zero, Vector3.Up);
            Game.Components.Add(_camera);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _modelSystem = new MyModel(Game.Content.Load<Model>(@"3DModel\Spaceship"), Matrix.Identity * Matrix.CreateTranslation(0, 0, -200));
            _modelUser = new MyModel(Game.Content.Load<Model>(@"3DModel\Spaceship"), Matrix.Identity * Matrix.CreateTranslation(0, -10, 0));

            base.LoadContent();
        } 


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            _collided = _modelUser.CheckCollision(_modelSystem.GetModel(), _modelSystem.GetWorld());
            if (_collided)
            {
                return;
            }

            KeyboardState keyboardState = Keyboard.GetState();

            // 根据用户的按键（上下左右），旋转 3D 模型 
            if (keyboardState.IsKeyDown(Keys.Up))
                _modelUser.Rotate(RotationDirection.Up);
            else if (keyboardState.IsKeyDown(Keys.Down))
                _modelUser.Rotate(RotationDirection.Down);
            else if (keyboardState.IsKeyDown(Keys.Left))
                _modelUser.Rotate(RotationDirection.Left);
            else if (keyboardState.IsKeyDown(Keys.Right))
                _modelUser.Rotate(RotationDirection.Right);

            // 根据用户的按键移动 3D 模型：LeftShift - 向前移动；LeftControl - 向后移动 
            if (keyboardState.IsKeyDown(Keys.LeftShift))
                _modelUser.Move(TranslationDirection.Forward);
            else if (keyboardState.IsKeyDown(Keys.LeftControl))
                _modelUser.Move(TranslationDirection.Backward); 


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (_collided)
            {
                Game.GraphicsDevice.Clear(Color.IndianRed);
            }
            else
            {
                Game.GraphicsDevice.Clear(Color.CornflowerBlue);
            }

            _modelSystem.Draw(_camera);
            _modelUser.Draw(_camera);

            base.Draw(gameTime);
        }
    }
}
